package com.imagecollector.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.imagecollector.common.constant.RedisConstant;
import com.imagecollector.common.utils.RedisUtils;
import com.imagecollector.domain.entity.District;
import com.imagecollector.domain.vo.DistrictVO;
import com.imagecollector.mapper.DistrictMapper;
import com.imagecollector.service.IDistrictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wrf
 * @since 2024-07-31
 */
@Service
public class DistrictServiceImpl extends ServiceImpl<DistrictMapper, District> implements IDistrictService {

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取所有区域及其子区域的 Map。
     *
     * @return 包含所有区域及其子区域的 Map，如果查询结果为空，则返回null。
     */
    @Override
    public Map<Integer, DistrictVO> allChildrenList() {
        // 查询所有区域
        List<District> districtList = lambdaQuery().list();
        // 如果查询结果为空，直接返回null
        if (CollectionUtil.isEmpty(districtList)) {
            return null;
        }

        Map<Integer, List<District>> districtMap = districtList.stream().collect(Collectors.groupingBy(District::getParentId));

        return districtList.stream().map(district -> new DistrictVO()
                .setId(district.getId())
                .setName(district.getName())
                .setParentId(district.getParentId())
                .setLevel(district.getLevel())
                .setIsSpecialCity(district.getIsSpecialCity())
                .setChildren(districtMap.getOrDefault(district.getId(), null)))
                .collect(Collectors.toMap(DistrictVO::getId, districtVO -> districtVO));
    }

    /**
     * 根据区域ID查询所有子区域列表
     *
     * @param districtId 父区域的ID
     * @return 包含所有子区域的列表
     */
    @Override
    public DistrictVO childrenList(Integer districtId) {
        if (redisUtils.hasKey(RedisConstant.DISTRICT + districtId)) {
            return redisUtils.get(RedisConstant.DISTRICT + districtId);
        }
        District districtParent = getById(districtId);
        Objects.requireNonNull(districtParent, "地区不存在");
        List<District> districtList = lambdaQuery().eq(District::getParentId, districtId).list();
        DistrictVO districtVO = new DistrictVO()
                .setId(districtParent.getId())
                .setName(districtParent.getName())
                .setParentId(districtParent.getParentId())
                .setLevel(districtParent.getLevel())
                .setIsSpecialCity(districtParent.getIsSpecialCity())
                .setChildren(districtList);
        redisUtils.set(RedisConstant.DISTRICT + districtId, districtVO);
        return districtVO;
    }

    @Override
    public String getDistrictFullName(Integer districtId) {
        StringBuilder fullName = new StringBuilder();
        if (redisUtils.hasKey(RedisConstant.DISTRICT + districtId)) {
            DistrictVO districtVO = redisUtils.get(RedisConstant.DISTRICT + districtId);
            while (districtVO.getParentId() != 0) {
                fullName.insert(0, districtVO.getName()).insert(0, "-");
                districtVO = redisUtils.get(RedisConstant.DISTRICT + districtVO.getParentId());
            }
            fullName.insert(0, districtVO.getName());
        }
        return fullName.toString();
    }

    @Override
    public List<District> getAllProvinces() {
        return lambdaQuery().eq(District::getParentId, 0).list();
    }
}
